package com.itheima.appserver.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.appserver.interceptor.UserThreadLocal;
import com.itheima.commons.constants.Constants;
import com.itheima.commons.model.domain.*;
import com.itheima.commons.model.vo.MangerUserVo;
import com.itheima.commons.model.vo.PageResult;
import com.itheima.dubbo.interfaces.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ManagerUserService {
    @Reference
    private UserApi userApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private UserLikeApi userLikeApi;

    @Reference
    private UserLoginRecordApi userLoginRecordApi;

    @Reference
    private UserFreezeApi userFreezeApi;

    @Reference
    private CountInfoApi countInfoApi;

    /**
     * 根据id 查询用户信息
     *
     * @param userId
     * @return
     */
    public MangerUserVo queryUserInfo(Long userId) {
        //根据id查询用户表
        User user = userApi.findUserById(userId);
        //根据id查询用户信息表
        UserInfo userInfo = userInfoApi.findUserInfoByUserId(userId);
        //被喜欢人数
        Long fanCount = userLikeApi.queryFanCount(userId);
        //喜欢人数
        Long likeCount = userLikeApi.queryLikeCount(userId);
        //互相喜欢数
        Long mutualLikeCount = userLikeApi.queryMutualLikeCount(userId);
        //用户登陆信息
        UserLoginRecord userLoginRecord = userLoginRecordApi.queryUserLoginRecordById(userId);
        //用户账号状态
        UserFreeze userFreeze = userFreezeApi.queryUserFreezeById(userId);
        //拼接vo
        MangerUserVo mangerUserVo = new MangerUserVo();
        mangerUserVo.setId(Convert.toInt(userInfo.getUserId()));
        mangerUserVo.setNickname(userInfo.getNickName());
        mangerUserVo.setMobile(user.getMobile());
        mangerUserVo.setSex(userInfo.getSex() == 1 ? "男" : "女");
        mangerUserVo.setPersonalSignature(null);
        mangerUserVo.setAge(userInfo.getAge());
        mangerUserVo.setCountBeLiked(Convert.toInt(fanCount));
        mangerUserVo.setCountLiked(Convert.toInt(likeCount));
        mangerUserVo.setCountMatching(Convert.toInt(mutualLikeCount));
        mangerUserVo.setIncome(Convert.toInt(userInfo.getIncome()));
        mangerUserVo.setOccupation(userInfo.getIndustry());
        mangerUserVo.setUserStatus(userFreeze.getUserStatus());
        mangerUserVo.setCreated(userInfo.getCreated().getTime());
        mangerUserVo.setCity(userInfo.getCity());
        mangerUserVo.setLastActiveTime(userLoginRecord.getLogtime().getTime());
        mangerUserVo.setLastLoginLocation(userLoginRecord.getPlace());
        mangerUserVo.setLogo(userInfo.getLogo());
        mangerUserVo.setTags(userInfo.getTags());
        return mangerUserVo;
    }

    /**
     * 用户冻结 查询用户冻结信息表
     *
     * @param userId
     * @param freezingTime
     * @param freezingRange
     * @param reasonsForFreezing
     * @param frozenRemarks
     */
    public String freezeUsers(String userId, String freezingTime, String freezingRange, String reasonsForFreezing, String frozenRemarks) {
        //先判断这个用户是是否存在
        UserFreeze userFreezeExists = userFreezeApi.queryUserFreezeById(Convert.toLong(userId));
        //定义冻结结束时间
        Long endTime = 0L;
        if ("1".equals(freezingTime)) {
            //冻结三天
            endTime = System.currentTimeMillis() + Constants.FREEZE_THREE_DAY;
        } else if ("2".equals(freezingTime)) {
            //冻结七天
            endTime = System.currentTimeMillis() + Constants.FREEZE_SEVEN_DAY;
        } else if ("3".equals(freezingTime)) {
            //永久冻结
            endTime = System.currentTimeMillis() + Constants.FREEZE_FOREVER;
        }
        UserFreeze userFreeze = new UserFreeze();
        userFreeze.setUserId(Convert.toInt(userId));
        userFreeze.setUserStatus("2");
        //冻结时长
        userFreeze.setFreezeTime(Convert.toInt(freezingTime));
        //冻结结束时间
        userFreeze.setFreezeEnd(endTime);
        //冻结原因
        userFreeze.setFreezeReason(reasonsForFreezing);
        //冻结备注
        userFreeze.setFreezeRemarks(frozenRemarks);
        //冻结范围
        userFreeze.setFreezeRange(freezingRange);
        //解冻原因
        userFreeze.setReasonsForThawing(null);
        //冻结开始时间
        userFreeze.setFreezeStart(new Date());
        //管理员id 谁操作的id
        userFreeze.setAdminId(UserThreadLocal.getUserId());
        userFreeze.setAdminIp("192.168.1.1");
        if (ObjectUtil.isNull(userFreezeExists)) {
            //执行新增操作
            userFreezeApi.freezeUsers(userFreeze);
            return "操作成功";
        } else {
            //执行修改操作
            userFreezeApi.updateFreezeUsers(userFreeze);
            return "操作成功";
        }
    }

    /**
     * 解冻用户
     *
     * @param userId
     * @param reasonsForThawing 解冻原因
     * @return
     */
    public String unfreezeUsers(String userId, String reasonsForThawing) {
        //查找之前存在数据库的记录 修改账户状态为1 添加解冻原因
        UserFreeze userFreeze = userFreezeApi.queryUserFreezeById(Convert.toLong(userId));
        if (ObjectUtil.isNull(userFreeze)) {
            return "操作失败";
        }
        userFreeze.setUserStatus("1");
        userFreeze.setReasonsForThawing(reasonsForThawing);
        //修改用户冻结表
        userFreezeApi.updateFreezeUsers(userFreeze);
        return "操作成功";
    }

    /**
     * 用户数据翻页接口
     *
     * @param page
     * @param pageSize
     * @param id
     * @param nickname
     * @param city
     * @return
     */
    public PageResult queryManagerUserByPage(Integer page, Integer pageSize, Long id, String nickname, String city) {
        List<UserInfo> userInfos = userInfoApi.queryUserInfoByPage(page, pageSize, id, nickname, city);
        Long count = userInfoApi.queryUserInfo(id, nickname, city);
        List<MangerUser> mangerUserList = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            MangerUser mangerUser = new MangerUser();
            mangerUser.setId(Convert.toInt(userInfo.getUserId()));
            mangerUser.setLogo(userInfo.getLogo());
            //头像状态 该字段暂时没有结果先固定赋值
            mangerUser.setLogoStatus("1");
            mangerUser.setNickname(userInfo.getNickName());
            User user = userApi.findUserById(userInfo.getUserId());
            mangerUser.setMobile(user.getMobile());
            mangerUser.setSex(userInfo.getSex() == 1 ? "男" : "女");
            mangerUser.setAge(userInfo.getAge());
            mangerUser.setOccupation(userInfo.getIndustry());
            UserFreeze userFreeze = userFreezeApi.queryUserFreezeById(userInfo.getUserId());
            mangerUser.setUserStatus(userFreeze.getUserStatus());
            mangerUser.setCity(userInfo.getCity());
            UserLoginRecord userLoginRecord = userLoginRecordApi.queryUserLoginRecordById(userInfo.getUserId());
            mangerUser.setLastActiveTime(userLoginRecord.getLogtime().getTime());
            mangerUserList.add(mangerUser);
        }
        return new PageResult(page,pageSize,count,mangerUserList);
    }
}
